var url = '/api/ajax_edit_user.cfm';
var url_get_all_users = '/api/ajax_get_users.cfm';
var id = {}; 
id.username = ' ';
id.old_password = '';
id.email = ' ';
function showBlank(str) {	
	if (str){return str;}else{return '';}
}
var progress_bar = function(wd,ht,txt,cl) {
	var throwaway = '<img src="/images/progress.gif" id="progress_bar" width="'+wd+'" height="'+ht+'" alt="'+txt+'" title="'+txt+'" align="absmiddle" />';	
	return throwaway;
}
var loading_bar = function(x,ele) {
	(x) ? $(ele).innerHTML =  progress_bar(16,16,'','blue') : $(ele).innerHTML = '';
}

var get_users = function() {
	loading_bar(1,'userhead_loading');
    $('#shopping_cart').html('<div align="center" class="Content_Margin">Loading Information<br />'+progress_bar(32,32,'')+'</div>');
    
	$.map($("input[class^='admin_profile']"), function(n) {
        $("#"+n.id).val('');
	});
    var local_array = [];
    $.ajax({
          type: 'GET',
          url: '/api/ajax_get_users.cfm',
          dataType: "json",
          success: function(json) {
              local_array = json.users;        
          },
          complete: function() {
                
            var result_text = '';
            
            $("#shopping_cart").html('<table width="100%" id="user_table" cellspacing="0" cellpadding="0" border="0" ></table>');
            
            $.map(local_array, function(n) {
                $("#user_table").append('<tr><td><a  href="#" id="view_' + n.username + '">' +  n.username + '</a>&nbsp;<img src="/images/image_delete.gif" border="0" alt="Delete From Report" title="Delete From Report" align="absmiddle" style=" cursor:pointer;" id="delete_user_' + n.username  + '" /><span class="Form_Item_Error" id="error_'+n.username+'"></span></td></tr>');
                $("#delete_user_"+ n.username).bind("click", function() {
                    delete_user(n.username);
                });
                $("#view_"+ n.username).bind("click", function() {
                    get_user_edit(n.username);
                });
            });
            loading_bar(false,'userhead_loading');
                           
          },
          error: function(json) {                
          }                
      });
}

function get_user_edit(id) {

    var local_array = [];
    $.ajax({
          type: 'GET',
          url: '/api/ajax_edit_user.cfm?action=edit_user&username='+id,
          dataType: "json",
          success: function(json) {
              local_array = json.users;        
          },
          complete: function() {
                show_edit(local_array);
                
          },
          error: function(json) {                
          }                
       });

}

var edit_user = function() {	
    $.map($("span[class^='Form_Item_Error']"), function(n) {
        $("#"+n.id).html('');
	});
    var local_array = [];
    $.ajax({
          type: 'GET',
          url: '/api/ajax_edit_user.cfm?action=save_edit&username='+$('#add_user_name').val()+'&pass1='+$('#add_password').val()+'&pass2='+$('#add_password2').val()+'&passnew='+$('#add_passwordnew').val()+'&email='+$('#add_email').val()+'&old_username='+$('#old_username').val(),
          dataType: "json",
          success: function(json) {
              local_array = json;        
          },
          complete: function() {
			  if(local_array.error){
				$.map(local_array.error, function(n) {
					$("#error_"+n.field).html(n.error_message);									  
				});
			}else{
               get_users();
               $('#add_password2').val('');
               $('#add_passwordnew').val('');
			}
          },
          error: function(json) {                
          }                
    });

}
var add_user = function() {	
	$.map($("span[class^='Form_Item_Error']"), function(n) {
        $("#"+n.id).html('');
	});
	loading_bar(true,'user_loading');
    var local_array = [];
    $.ajax({
          type: 'GET',
          url: '/api/ajax_edit_user.cfm?action=add_user&username='+$('#add_user_name').val()+'&pass1='+$('#add_password').val()+'&pass2='+$('#add_password2').val()+'&email='+$('#add_email').val(),
          dataType: "json",
          success: function(json) {
              local_array = json;
			
          },
          complete: function() {
			if(local_array.error){
				$.map(local_array.error, function(n) {
					$("#error_"+n.field).html(n.error_message);									  
				});
			}else{
            	get_users();
            	$('#add_password2').val(''); 
			}
			loading_bar(false,'user_loading');
          },
          error: function(json) { 
		  	
          }                
    });
}

var delete_user = function(id) {	
	$.map($("span[class^='Form_Item_Error']"), function(n) {
        $("#"+n.id).html('');
	});
    var local_array = [];
    $.ajax({
          type: 'GET',
          url: '/api/ajax_edit_user.cfm?action=delete_user&username='+id,
          dataType: "json",
          success: function(json) {
             local_array = json;
             if (local_array.error) {
                $.map(local_array.error, function(n) {
                    $("#error_"+n.field).html(n.error_message);
                });
             }   
          },
          complete: function() {
              get_users();
          },
          error: function(json) {                
          }                
    });
}

function show_edit(id) {
	$("#user_area").html('<table width="100%"><tr><td></td><td align="left"><h3><strong>Edit User</strong></h3></td></tr><tr><td align="right" width="210px"><strong>User Name:</strong></td><td align="left" width="600px"><input class="admin_profile" type="text" name="add_user_name"id="add_user_name" size="25" value="'+showBlank(id[0].selected_username)+'"><span class="Form_Item_Error" id="error_add_user_name"></span></td></tr><tr><td align="right"><strong>Old Password:</strong></td><td align="left" width="600px"><input class="admin_profile" type="password" name="add_password"id="add_password" size="25"  value="'+showBlank(id[0].selected_password)+'"><span class="Form_Item_Error" id="error_add_password"></span></td></tr><tr><td align="right"><strong>New Password:</strong></td><td align="left" width="600px"><input class="admin_profile" type="password" name="add_password2"id="add_password2" size="25"  value=""><span class="Form_Item_Error" id="error_add_password2"></span></td></tr><tr><td align="right"><strong>New Password Again:</strong></td><td align="left" width="600px"><input class="admin_profile" type="password" name="add_passwordnew"id="add_passwordnew" size="25"  value=""><span class="Form_Item_Error" id="error_add_passwordnew"></span></td></tr><tr><td align="right"><strong>Email:</strong></td><td align="left" width="600px"><input class="admin_profile" type="text" name="add_email"id="add_email" size="25" value="'+showBlank(id[0].selected_email)+'"><span class="Form_Item_Error" id="error_email"></span></td></tr><tr><td align="right"> </td><td align="left" width="600px"><input type="submit" value="Save User" name="edit_user"id="edit_user" class="button"><span id="user_loading" class="loading"></span><span class="Form_Item_Error" id="ajax_message_01"></span></td></tr></table><input type="hidden" id="old_username" value="'+id[0].selected_username+'">');
    $("#edit_user").bind("click", function() {
        edit_user();
    });

}

function show_add() {
	$("#user_area").html('<table width="100%"><tr><td  ></td><td align="left"><h3><strong>Add New User</strong></h3></td></tr><tr><td align="right" width="210px"><strong>User Name:</strong></td><td align="left" width="600px"><input class="admin_profile" type="text" name="add_user_name"id="add_user_name" size="25"  value=""><span class="Form_Item_Error" id="error_add_user_name"></span></td></tr><tr><td align="right"><strong>Password:</strong></td><td align="left" width="600px"><input class="admin_profile" type="password" name="add_password"id="add_password" size="25"  value=""><span class="Form_Item_Error" id="error_add_password"></span></td></tr><tr><td align="right"><strong>Password Again:</strong></td><td align="left" width="600px"><input class="admin_profile" type="password" name="add_password2"id="add_password2" size="25"  value=""><span class="Form_Item_Error" id="error_add_password2"></span></td></tr><tr><td align="right"><strong>Admin Email:</strong></td><td align="left" width="600px"><input class="admin_profile" type="text" name="add_email"id="add_email" size="25" value=""><span class="Form_Item_Error" id="error_add_email"></span></td></tr><tr><td align="right">&nbsp;</td><td align="left" width="600px"><input type="button" value="Add New User" name="add_user"id="add_user" class="button"><span id="user_loading" class="loading"></span>&nbsp;<span class="Form_Item_Error" id="ajax_message_01"></span></td></tr></table>');
	$("#add_user").bind("click", function() {
        add_user();
    });
}

$(function() {
	Functional.install();
    show_add();
    get_users();
	$("#add_new_user").bind("click", function() {
        show_add();
	});
	
});